package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.PanelConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;

/**
 * @User: Yr
 * @Date: 2021/12/13
 * @Description:
 */
@Service
public class IHomeServiceImpl implements IHomeService {


    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    PanelConverter panelConverter;
    @Autowired
    ContentConverter contentConverter;

    @Override
    public HomePageResponse homepage() {
        HomePageResponse response = new HomePageResponse();
        try {
            List<Panel> panels = panelMapper.selectAll();
            List<PanelDto> panelDtos = panelConverter.panels2Dto(panels);
            HashSet hashSet = new HashSet();
            for (PanelDto panelDto : panelDtos) {
                if (panelDto.getId() != 6){
                    List<PanelContentItem> panelContentItems =
                            panelContentMapper.selectPanelContentAndProductWithPanelId(panelDto.getId());
                    List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                    panelDto.setPanelContentItems(panelContentItemDtos);
                    hashSet.add(panelDto);
                }
            }
            response.setPanelContentItemDtos(hashSet);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return response;
    }
}
